﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Newtonsoft.Json;
using Share;
using Newtonsoft.Json.Converters;
using System.Runtime.Serialization;

namespace NewtonJsonUsage
{
    class Program
    {
        static void Main(string[] args)
        {
            // 序列化（官网）
            SerializeObject.Demo();
            
            // 反序列化（官网）
            DeserializeObject.Demo();

            // Linq to json（官网）
            LINQToJson.Demo();

            ToFile.Demo();


            User u = new User
            {
                UserName = "Fan",
                Status = UserStatus.Active
            };

            Console.WriteLine(JsonConvert.SerializeObject(u));           

        }
    }

    public enum UserStatus
    {
        NotConfirmed,
        Active,
        Deleted
    }

    public class User
    {
        
        public string UserName { get; set; }

        [JsonConverter(typeof(StringEnumConverter))] // 对枚举序列化名称，没有此特性则序列化数字
        public UserStatus Status { get; set; }

        [OnSerializing]
        internal void Method1(StreamingContext context)
        {
            Console.WriteLine("start");
        }

        [OnSerialized]
        internal void Method2(StreamingContext context)
        {
            Console.WriteLine("end");
        }

        [OnDeserializing]
        internal void OnDeserializingMethod(StreamingContext context)
        {
            Console.WriteLine("This value was set during deserialization");
        }

        [OnDeserialized]
        internal void OnDeserializedMethod(StreamingContext context)
        {
            Console.WriteLine("This value was set after deserialization.");
        }
    }
}
